import Cache from "/Config/Cache";
import { createClient, RedisClientType } from "redis";

export default function CacheBase() {
    const select = Cache.select;
    const stores = Cache.stores;
    const cache = stores[select];
    if (cache.type === 'redis') {
        // 创建 Redis 客户端
        const options: Record<string, any> = {
            socket: {
                host: cache.host,
                port: cache.port,
            },
            password: cache.password
        };
        if (!(!cache.username || cache.username == 'default')) options.username = cache.username;
        const client = createClient(options);
        client.on('error', (err) => logger.error('Redis Client Error', err));
        client.on('connect', () => logger.info('Redis Client Connected'));
        client.connect();
        global.redis = client as RedisClientType;//绑定到nodejs 全局

    }
}